package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.exception.Assert;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.BatchQueryTributPowerListReq;
import com.sg.dto.biz.framework.req.QueryPlanKpiTempMgtPowerListComReqDto;
import com.sg.dto.biz.framework.req.QueryRoleOfPowerListComReqDto;
import com.sg.dto.biz.framework.req.TributPowerDto;
import com.sg.dto.biz.framework.res.QueryPlanKpiTempMgtPowerListComRespDto;
import com.sg.dto.biz.framework.res.QueryRoleOfPowerListComRespDto;
import com.sg.entity.PrivacyDistribution;
import com.sg.service.base.framework.MPrivacyDistributionService;
import com.sg.service.biz.framework.EvaSystemService;
import com.sg.service.biz.framework.nb.NbEvaSystem;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class EvaSystemServiceImpl implements EvaSystemService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MPrivacyDistributionService mPrivacyDistributionService;
    @Resource
    private NbEvaSystem nbEvaSystem;

    /**
     * D1-2查询绩效规划模板管理权限列表(公共)[3096]
     * gen by moon at 5/21/2024, 2:02:22 AM
     */
    @Trace(operationName = "D1-2查询绩效规划模板管理权限列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryPlanKpiTempMgtPowerListComRespDto queryPlanKpiTempMgtPowerListCom(QueryPlanKpiTempMgtPowerListComReqDto reqDto) {


        List<PrivacyDistribution> listPrivacyDistribution_1 = new ArrayList<>();
//步骤0: D1查询用于权限角色列表(公共) - queryRoleOfPowerListCom
        //ModelCode: getUserRoleListFromRedis
        QueryRoleOfPowerListComRespDto getUserRoleListFromRedisRes = null;
        QueryRoleOfPowerListComReqDto getUserRoleListFromRedisReq = new QueryRoleOfPowerListComReqDto();
        getUserRoleListFromRedisReq.setOriginalRoleMemberId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecOriginalRoleMemberId());//CURRENT_ORIGINAL_ROLE_MEMBERID//sourceId:104952_1
        getUserRoleListFromRedisReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1811484_1

        /*D1查询用于权限角色列表(公共)[2189]  用于登录时，异步获取登录用户所属的角色信息列表 */
        Assert.isNull(getUserRoleListFromRedisReq.getOriginalRoleMemberId(), "D1-2查询绩效规划模板管理权限列表(公共)-D1查询用于权限角色列表(公共)-身份人员ID不能为空", false);
        Assert.isNull(getUserRoleListFromRedisReq.getSubjectLifeCycle(), "D1-2查询绩效规划模板管理权限列表(公共)-D1查询用于权限角色列表(公共)-主体生命周期不能为空", false);
        getUserRoleListFromRedisRes = nbEvaSystem.queryRoleOfPowerListCom(getUserRoleListFromRedisReq);


//步骤1: 1-2-12批量查询权限分配列表 - batchQueryTributPowerList
        List<PrivacyDistribution> listPrivacyDistribution = new ArrayList<>();
        BatchQueryTributPowerListReq batchQueryTributPowerListReq = new BatchQueryTributPowerListReq();
        batchQueryTributPowerListReq.setIsDataPrivacy("TRUE");//sourceId:104958_1
        batchQueryTributPowerListReq.setPrivacyCategory("SUB_DATA_PRIVACY");//sourceId:104959_1
        batchQueryTributPowerListReq.setBusinessPurposes("ALL");//sourceId:105454_1
        batchQueryTributPowerListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:104956_1
        if (getUserRoleListFromRedisRes != null && getUserRoleListFromRedisRes.getRoleRelList() != null && !CollectionUtil.isEmpty(getUserRoleListFromRedisRes.getRoleRelList())) {
            batchQueryTributPowerListReq.setTributPowerList(getUserRoleListFromRedisRes.getRoleRelList().stream().map(item -> item.getRoleId())
                    .collect(Collectors.toList()));/*list-to-strings*///sourceId:104954_1
        }
        if (reqDto != null) {
            batchQueryTributPowerListReq.setTypeCode(reqDto.getTypeCode());//SimpleFieldAssign//sourceId:104953_1
            batchQueryTributPowerListReq.setDataModel(reqDto.getDataModel());//SimpleFieldAssign//sourceId:104960_1
        }

        /*1-2-12批量查询权限分配列表[702]   */
        Assert.isNull(batchQueryTributPowerListReq.getTypeCode(), "D1-2查询绩效规划模板管理权限列表(公共)-1-2-12批量查询权限分配列表-分配对象类型编码不能为空", false);
        Assert.isNull(batchQueryTributPowerListReq.getIsDataPrivacy(), "D1-2查询绩效规划模板管理权限列表(公共)-1-2-12批量查询权限分配列表-是否数据权限不能为空", false);
        Assert.isNull(batchQueryTributPowerListReq.getDataModel(), "D1-2查询绩效规划模板管理权限列表(公共)-1-2-12批量查询权限分配列表-数据父模型编码不能为空", false);
        Assert.isNull(batchQueryTributPowerListReq.getPrivacyCategory(), "D1-2查询绩效规划模板管理权限列表(公共)-1-2-12批量查询权限分配列表-权限类别不能为空", false);
        Assert.isNull(batchQueryTributPowerListReq.getBusinessPurposes(), "D1-2查询绩效规划模板管理权限列表(公共)-1-2-12批量查询权限分配列表-权限业务用途不能为空", false);
        Assert.isNull(batchQueryTributPowerListReq.getSubjectLifeCycle(), "D1-2查询绩效规划模板管理权限列表(公共)-1-2-12批量查询权限分配列表-主体生命周期不能为空", false);
        listPrivacyDistribution = mPrivacyDistributionService.batchQueryTributPowerList(batchQueryTributPowerListReq)/*vcase invoke 本地 method 方法调用;*/;


        listPrivacyDistribution_1 = listPrivacyDistribution;

        QueryPlanKpiTempMgtPowerListComRespDto retData = new QueryPlanKpiTempMgtPowerListComRespDto();
        retData.setTributPowerList(listPrivacyDistribution_1.stream().map(item -> BeanUtil.toBean(item, TributPowerDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:104961_1


        return retData;
    }
    //
}
